package modele;

import java.io.Writer;
import java.rmi.server.UnicastRemoteObject;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

public class EtudiantImpl extends UnicastRemoteObject implements Etudiant {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8L;

	Date date;
	String idApo;
	String etatCivil, nom, prenom, email, telephone;
	String adresse;
	String ville, pays;
	boolean redoublant, formationContinue;
	String origine;
	private String[][] cursus;

	public EtudiantImpl(Dossier dos) throws Exception {
		idApo = new String("X");
		date = new Date();
		etatCivil = new String("Monsieur");
		nom = new String("");
		prenom = new String("");
		email = new String("");
		telephone = new String("");
		adresse = new String("");
		ville = new String("");
		pays = new String("France");
		redoublant = false;
		formationContinue = false;
		origine = new String("");
		cursus = new String[4][2];
		int annee = Calendar.getInstance().get(Calendar.YEAR);
		for (int i=0; i<cursus.length; i++) {
			cursus[i][0] = new String((annee-i-1)+"-"+(annee-i));
			cursus[i][1] = new String("");
		}
	}
	
	//TODO pour les autres parcours
	private String getCursusString(String activite, String etab) {
		String s= new String("");
		if(etab.contentEquals("0331764N")) {	//Bordeaux 1
			if(activite.contentEquals("Master 1 Informatique, spécialité algorithmes et méthodes formelles, parcours european master in software verification"))
				s=diplome[6];
			if(activite.contentEquals("Licence 3 STS Informatique (semestres 1 à 6)"))
				s=diplome[10];
			if(activite.contentEquals("Licence 2 STS Informatique (semestres 1 à 4)"))
				s=diplome[10];
			if(activite.contentEquals("Master 2 professionnel mention MIAGE, spécialité systèmes d'information des entreprises"))
				s=diplome[21];
		}
		return s;
	}

	public EtudiantImpl(Dossier dos, Map<String,String> fields) throws Exception {
		//me = dos;
		date = new Date();
		idApo = new String(fields.get("NO_DOSSIER"));
		etatCivil = new String(fields.get("CIVILITE"));
		nom = new String(fields.get("NOM"));
		prenom = new String(fields.get("PRENOM"));
		email = new String(fields.get("EMAIL"));
		telephone = new String(fields.get("TELEPHONE")+" - "+fields.get("TELEPHONE_PORTABLE"));
		adresse = new String(fields.get("ADRESSE"));
		ville = new String(fields.get("VILLE"));
		pays = new String(fields.get("PAYS_ADRESSE"));
		redoublant = false;
		formationContinue = false; // pas dans Apoflux
		origine = new String(fields.get("LIB_AN_1_ETAB"));
		cursus = new String[4][2];
		int annee = Calendar.getInstance().get(Calendar.YEAR);
		for (int i=0; i<cursus.length; i++) {
			cursus[i][0] = new String((annee-i-1)+"-"+(annee-i));
			cursus[i][1] = new String("");
		}
		//année 1
		cursus[0][1]=getCursusString(fields.get("AN_1_ACTIVITE"), fields.get("AN_1_ETAB"));
		cursus[1][1]=getCursusString(fields.get("AN_2_ACTIVITE"), fields.get("AN_2_ETAB"));
		cursus[2][1]=getCursusString(fields.get("AN_3_ACTIVITE"), fields.get("AN_2_ETAB"));
	}
	
	public String getNomCursusAnnée1() throws Exception {
		return cursus[0][1];
	}
	
	public String getNomCursusAnnée2() throws Exception {
		return cursus[1][1];
	}
	
	public String getNomCursusAnnée3() throws Exception {
		return cursus[2][1];
	}
	
	public String getNomCursusAnnée4() throws Exception {
		return cursus[3][1];
	}
	
	public String getAnnéeCursusAnnée1() throws Exception {
		return cursus[0][0];
	}
	
	public String getAnnéeCursusAnnée2() throws Exception {
		return cursus[1][0];
	}
	
	public String getAnnéeCursusAnnée3() throws Exception {
		return cursus[2][0];
	}
	
	public String getAnnéeCursusAnnée4() throws Exception {
		return cursus[3][0];
	}
	
	public void setCursusAnnee1(String annee, String nom) throws Exception {
		cursus[0][0] = annee;
		cursus[0][1] = nom;
	}
	
	public void setCursusAnnee2(String annee, String nom) throws Exception {
		cursus[1][0] = annee;
		cursus[1][1] = nom;
	}
	
	public void setCursusAnnee3(String annee, String nom) throws Exception {
		cursus[2][0] = annee;
		cursus[2][1] = nom;
	}
	
	public void setCursusAnnee4(String annee, String nom) throws Exception {
		cursus[3][0] = annee;
		cursus[3][1] = nom;
	}
	
	public String getIdApoflux() throws Exception {
		return idApo;
	}
	
	// pour creer un clone de la fiche etudiant
	public EtudiantImpl(Etudiant etu) throws Exception {
		date = etu.getDate();
		idApo = etu.getIdApoflux();
		etatCivil = new String(etu.getEtatCivil());
		nom = new String(etu.getNom());
		prenom = new String(etu.getPrenom());
		email = new String(etu.getEmail());
		telephone = new String(etu.getTelephone());
		adresse = new String(etu.getAdresse());
		ville = new String(etu.getVille());
		pays = new String(etu.getPays());
		redoublant = new Boolean(etu.getRedoublant());
		formationContinue = new Boolean(etu.getFormationContinue());
		origine = new String(etu.getEtablissementOrigine());
		cursus[0][1] = etu.getNomCursusAnnée1();
		cursus[1][1] = etu.getNomCursusAnnée2();
		cursus[2][1] = etu.getNomCursusAnnée3();
		cursus[0][0] = etu.getAnnéeCursusAnnée1();
		cursus[1][0] = etu.getAnnéeCursusAnnée2();
		cursus[2][0] = etu.getAnnéeCursusAnnée3();
	}

	public Date getDate() throws Exception {
		return date;
	}

	public String getEtatCivil() throws Exception {
		return etatCivil;
	}

	public String getNom() throws Exception {
		return nom;
	}

	public String getPrenom() throws Exception {
		return prenom;
	}

	public String getEmail() throws Exception {
		return email;
	}

	public String getTelephone() throws Exception {
		return telephone;
	}

	public String getAdresse() throws Exception {
		return adresse;
	}

	public String getVille() throws Exception {
		return ville;
	}

	public String getPays() throws Exception {
		return pays;
	}

	public boolean getRedoublant() throws Exception {
		return redoublant;
	}

	public boolean getFormationContinue() throws Exception {
		return formationContinue;
	}

	public String getEtablissementOrigine() throws Exception {
		return origine;
	}

	public void setDate(Date d) throws Exception {
		date = d;
	}

	public void setEtatCivil(String st) throws Exception {
		etatCivil = st;
	}

	public void setNom(String st) throws Exception {
		nom = st.trim();
	}

	public void setPrenom(String st) throws Exception {
		prenom = st.trim();
	}

	public void setEmail(String st) throws Exception {
		email = st.trim();
	}

	public void setTelephone(String st) throws Exception {
		telephone = st.trim();
	}

	public void setAdresse(String st) throws Exception {
		adresse = st.trim();
	}

	public void setVille(String st) throws Exception {
		ville = st.trim();
	}

	public void setPays(String st) throws Exception {
		pays = st.trim();
	}

	public void setRedoublant(boolean b) throws Exception {
		redoublant = b;
	}

	public void setFormationContinue(boolean b) throws Exception {
		formationContinue = b;
	}

	public void setEtablissementOrigine(String st) throws Exception {
		origine = st;
	}

	public void imprimer(Writer file) 
	throws Exception {
		file.write("\\caption*{\\bf{\\'Etat civil}}\\\\\\hline\n"+
				etatCivil+" "+nom+" "+prenom+"&Etablissement d'origine : "+getEtablissementOrigine()+"\\\\\n"+
				adresse+"&~\\\\\n"+
				ville+"&Email : \\verb!"+email+"!\\\\\n"+
				pays+"&T\\'el\\'ephone : "+telephone+"\\\\\\hline\n"+
				"\\caption*{\\bf{\\'Etudes}}\\\\\\hline\n"+
				"Cursus actuel en "+cursus[0][0]+"&"+cursus[0][1]+"\\\\\n"+
				"Cursus année - 1 en "+cursus[1][0]+
				"&"+cursus[1][1]+"\\\\\n"+
				"Cursus année - 2 en "+cursus[2][0]+
				"&"+cursus[2][1]+"\\\\\n"+
				"Cursus année - 3 en "+cursus[3][0]+
				"&"+cursus[3][1]+"\\\\\\hline\n");
	}

	public void imprimerCourrier(Writer file)
	throws Exception {
		file.write("\\renewcommand{\\getEtatCivil}{"+getEtatCivil()+"}\n"+
				"\\renewcommand{\\getNom}{"+getNom()+"}\n"+
				"\\renewcommand{\\getPrenom}{"+getPrenom()+"}\n");

		file.write("\\renewcommand{\\getAdresse}{"+getAdresse()+"}\n");

		file.write("\\renewcommand{\\getCodePostal}{}\n");

		file.write("\\renewcommand{\\getVille}{"+getVille()+"}\n"+
				"\\renewcommand{\\getPays}{"+getPays()+"}\n");
		file.write("\\renewcommand{\\getIdent}{"+getIdApoflux()+"}\n");

		if (etatCivil.equals("Monsieur"))
			file.write("\\renewcommand{\\getOuverture}"+
			"{Cher candidat,}\n");
		else
			file.write("\\renewcommand{\\getOuverture}"+
			"{Ch\\`ere candidate,}\n");
		file.write("\\begin{letter}{\n"+
				"\\getEtatCivil\\ \\getNom\\ \\getPrenom\\\\\n"+
				"\\getAdresse\\\\\n"+
				"\\getCodePostal\\ \\getVille\\\\\n"+
				"\\getPays}\n"+
				"\\toref{\\shortstack[l]{"+
				"Num\\'ero de dossier : \\getIdent\\\\\n"+
				"Proposition n$^o$ : \\getNumProp}}\n"+
		"\\opening{\\getOuverture}\n");
	}

}
